﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.IO;

namespace ConsolidationTool.WebParts.ControlTemplates.FS.Template
{
    public partial class KoCommonTemplate : UserControl
    {
        public string ListTitle { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        { 
            int LastIndexOf = ListTitle.LastIndexOf('_');
            string partFix = ListTitle.Substring(0, LastIndexOf);

            string ascxUrl = @"~/_CONTROLTEMPLATES/FS.Template/" + partFix + "_Template/" + ListTitle + ".ascx";
            string ascxPath = Server.MapPath(ascxUrl);
            string lastFixAscxUrl = @"~/_CONTROLTEMPLATES/FS.Template/KoTemplate/" + partFix + "_Base.ascx";
            string lastFixAscxPath = Server.MapPath(lastFixAscxUrl);
            string firstFixAscxUrl = @"~/_CONTROLTEMPLATES/FS.Template/KoTemplate/" + ListTitle.Substring(0, ListTitle.IndexOf('_')) + "_Base.ascx";
            string firstFixAscxPath = Server.MapPath(firstFixAscxUrl);


            if (File.Exists(ascxPath))
                template_area.Controls.Add(Page.LoadControl(ascxUrl)); 
            else if(File.Exists(lastFixAscxPath))
                template_area.Controls.Add(Page.LoadControl(lastFixAscxUrl));
            else
                template_area.Controls.Add(Page.LoadControl(firstFixAscxUrl));
             
        }
    }
}
